package models

import (
	"fmt"
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
)

type UpdateCarrierOwnerRQ struct {
	VehicleNo         string `json:"vehicle_no"`
	VehicleType       int    `json:"vehicle_type"`
	VehicleLength     int    `json:"vehicle_len"`
	VehicleLoad       int    `json:"vehicle_load"`
	FrequentLineStart string `json:"frequent_line_start"`
	FrequentLineEnd   string `json:"frequent_line_end"`
	ShipName          string `json:"ship_name"`
	ShipMMSI          string `json:"ship_mmsi"`
	ShipExDate        string `json:"ship_ex_date"`
	ShipHarbour       string `json:"ship_harbour"`
	ShipWidth         string `json:"ship_width"`
	ShipDepth         string `json:"ship_depth"`
	ShipCapacity      string `json:"ship_capacity"`
	ShipFullLoad      string `json:"ship_full_load"`
	ShipEmptyLoad     string `json:"ship_empty_load"`
	ShipHatchway      string `json:"ship_hatchway"`
}
type CarrierOwnerRS struct {
	Name               string `json:"name"`
	Stars              int    `json:"stars"`
	Score              string `json:"score"`
	Phone              string `json:"phone"`
	Photo              string `json:"photo"`
	VehicleType        int    `json:"vehicle_type"`
	VehcileLen         int    `json:"vehicle_len"`
	VehicleLoad        int    `json:"vehicle_load"`
	VehicleNo          string `json:"vehicle_no"`
	VehcilePhotoUrl    string `json:"vehicle_photo_url"`
	DrivingLicenseUrl  string `json:"driving_license_url"`
	VehicleLicenseUrl1 string `json:"vehicle_license_1_url"`
	VehicleLicenseUrl2 string `json:"vehicle_license_2_url"`
	FrequentLineStart  string `json:"frequent_line_start"`
	FrequentLineEnd    string `json:"frequent_line_end"`
	ShipName           string `json:"ship_name"`
	ShipMMSI           string `json:"ship_mmsi"`
	ShipExDate         string `json:"ship_ex_date"`
	ShipHarbour        string `json:"ship_harbour"`
	ShipWidth          string `json:"ship_width"`
	ShipDepth          string `json:"ship_depth"`
	ShipCapacity       string `json:"ship_capacity"`
	ShipFullLoad       string `json:"ship_full_load"`
	ShipEmptyLoad      string `json:"ship_empty_load"`
	ShipHatchway       string `json:"ship_hatchway"`
	CreateTime         string `json:"create_time"`
}

func (m Member) UpdateCarrierOwner(mbrId ID, rq UpdateCarrierOwnerRQ) (err error) {
	var id int64
	var repo data.MemberRepo
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model - UpdateCarrierOwner] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		err = repo.UpdateCarrierOwner(&data.CarrierOwnerDto{
			Id:                int(id),
			VehicleType:       rq.VehicleType,
			VehicleLength:     rq.VehicleLength,
			VehicleLoad:       rq.VehicleLoad,
			VehicleNo:         rq.VehicleNo,
			FrequentLineStart: rq.FrequentLineStart,
			FrequentLineEnd:   rq.FrequentLineEnd,
			ShipName:          rq.ShipName,
			ShipMMSI:          rq.ShipMMSI,
			ShipExDate:        rq.ShipExDate,
			ShipHarbour:       rq.ShipHarbour,
			ShipWidth:         rq.ShipWidth,
			ShipDepth:         rq.ShipDepth,
			ShipCapacity:      rq.ShipCapacity,
			ShipFullLoad:      rq.ShipFullLoad,
			ShipEmptyLoad:     rq.ShipEmptyLoad,
			ShipHatchway:      rq.ShipHatchway,
		})
		if err != nil {
			util.GetLogger().Error("[model - UpdateCarrierOwner] - error: %s", err.Error())
			err = errors.New(errors.MSG_Mbr_Upd_Carrier_Owner_Error)
		}
	}
	return err
}

func (m Member) GetCarrierOwner(mbrId ID) (rs CarrierOwnerRS, err error) {
	var id int64
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-GetCarrierOwner] - unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		var repo data.MemberRepo
		if dto, err := repo.GetCarrierOwner(int(id)); err == nil {
			rs = CarrierOwnerRS{
				Name:               dto.Name,
				Stars:              dto.Stars,
				Phone:              dto.Phone,
				Photo:              dto.Photo,
				VehicleType:        dto.VehicleType,
				VehcileLen:         dto.VehcileLen,
				VehicleLoad:        dto.VehicleLoad,
				VehicleNo:          dto.VehicleNo,
				VehcilePhotoUrl:    dto.VehcilePhotoUrl,
				DrivingLicenseUrl:  dto.DrivingLicenseUrl,
				VehicleLicenseUrl1: dto.VehicleLicenseUrl1,
				VehicleLicenseUrl2: dto.VehicleLicenseUrl2,
				FrequentLineStart:  dto.FrequentLineStart,
				FrequentLineEnd:    dto.FrequentLineEnd,
				CreateTime:         dto.CreateTime,
				ShipName:           dto.ShipName,
				ShipMMSI:           dto.ShipMMSI,
				ShipExDate:         dto.ShipExDate,
				ShipHarbour:        dto.ShipHarbour,
				ShipWidth:          dto.ShipWidth,
				ShipDepth:          dto.ShipDepth,
				ShipCapacity:       dto.ShipCapacity,
				ShipFullLoad:       dto.ShipFullLoad,
				ShipEmptyLoad:      dto.ShipEmptyLoad,
				ShipHatchway:       dto.ShipHatchway,
			}
			if (dto.ScoreCount) == 0 {
				rs.Score = "0"
			} else {
				rs.Score = fmt.Sprintf("%.0f", float32(dto.ScoreTotal)/float32(dto.ScoreCount))
			}
		} else {
			util.GetLogger().Error("[model-GetCarrierOwner] - error: %s", err.Error())
			err = nil
		}
	}
	return
}
